package com.js.baseview.selector.adapter;

// Created by JS on 2020/8/28.

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.js.baseview.R;
import com.js.baseview.selector.entity.SelectionItem;

import java.util.List;

public class SelectionAdapter extends BaseAdapter {

    private Context mContext;
    private List<SelectionItem> mItems;

    public SelectionAdapter(Context context, List<SelectionItem> items) {
        mContext = context;
        mItems = items;
    }

    @Override
    public int getCount() {
        return mItems == null ? 0 : mItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView == null) {
            view = View.inflate(mContext, R.layout.item_simple_selecion, null);
            holder = new ViewHolder();

            holder.tvItemName = view.findViewById(R.id.tv_item_name);
            view.setTag(holder);

        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        SelectionItem item = mItems.get(position);
        holder.tvItemName.setText(item.getData());
        return view;
    }

    public void setItems(List<SelectionItem> items) {
        mItems = items;
        notifyDataSetChanged();
    }

    class ViewHolder {
        TextView tvItemName;
    }
}
